package com.neborosoft.lib.collections.list;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * Project: FlyingDog
 * User: stikhonenko
 * Date: 12/10/12
 * Time: 3:59 PM
 */
public class UniqueSortedList<T> extends SortedList<T>{
    @Override
    public boolean add(T object) {
        int index = indexOf(object);
        if(index < 0){
            index = -index - 1;
            arrayListAdd(index, object);
            return true;
        }
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        boolean listModified = false;

        for(T object : collection){
            if(add(object)){
                listModified = true;
            }
        }

        return listModified;
    }

    @Override
    protected void onSetComparator() {
        if(isEmpty()){
            return;
        }

        List<T> clone = Arrays.asList((T[])toArray());
        clear();
        addAll(clone);
    }

    public UniqueSortedList(Collection<? extends T> collection, Comparator<T> comparator) {
        super(collection, comparator);
    }

    public UniqueSortedList(Collection<? extends T> collection) {
        super(collection);
    }
}
